package edu.upb.acs.jobsmaps.util;

import android.app.Activity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import edu.upb.acs.jobsmaps.activity.add.AddJobActivityVoice;
import edu.upb.acs.jobsmaps.database.task.GetCompaniesTask;
import edu.upb.acs.jobsmaps.entity.Company;
import edu.upb.acs.jobsmaps.entity.CompanyIdentifier;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by dani on 6/28/2014.
 */
public class Companies {
	private static final String TAG = AddJobActivityVoice.class.getName();

	public static void populateSpinnerCompanies(Spinner spinner, Activity activity) {
		GetCompaniesTask getCompaniesTask = new GetCompaniesTask();
		try {
			Collection<Company> companies = getCompaniesTask.execute().get();
			Log.d(TAG, "Got " + companies.size() + " from the database");

//			Spinner spinner = ;
			List<CompanyIdentifier> companiesIdentifiers = new ArrayList<>();
			for (Company company : companies) {
				companiesIdentifiers.add(new CompanyIdentifier(company.getName(), company.getId()));
			}
			ArrayAdapter<CompanyIdentifier> dataAdapter = new ArrayAdapter<>(activity, android.R.layout.simple_spinner_item, companiesIdentifiers);
			dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
			spinner.setAdapter(dataAdapter);

		} catch (Exception e) {
			Log.e(TAG, "Can not get companies", e);
		}
	}
}
